<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="item in levelList" :key="item.path">
        <span>{{item.title}}</span>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>
import {mapGetters} from 'vuex'

export default {
  
  created() {
      this.getBreadcrumb()
  },
  data() {
    return {
      levelList: [
      ]
    }
  },
  computed: {
      ...mapGetters(['routers'])
  },
  watch: {
    $route() {
        this.getBreadcrumb()
    }
  },
  computed: {
      ...mapGetters(['routers'])
  },
  methods: {
      getBreadcrumb() {

          this.levelList = []
      
          let path = this.$route.path
          if(path && path.length <= 1) {
              return 
          }
          let pathArr = path.split('/')
          if(pathArr.length < 2) {
            return 
          }

          let parentPathStr = '/' + pathArr[1]
          let matched = this.routers.filter(item => item.path === parentPathStr)
          if(matched && matched.length > 0) {
              
              let parentPath = matched[0]
              
              this.levelList.push({title: parentPath.meta.title, path: parentPath.path})

              if(parentPath.children) {
                  
                  let childMatched = parentPath.children.filter(item => item.path === path)

                  if(childMatched) {
                      this.levelList.push({title: childMatched[0].meta.title, path: childMatched[0].path})    
                  }
              }
          }
      }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-breadcrumb.el-breadcrumb {
    display: inline-block;
    font-size: 14px;
    line-height: 50px;
    margin-left: 10px;
    .no-redirect {
      color: #97a8be;
      cursor: text;
    }
  }
</style>
